package com.jsits.v2x.common.connection.socket;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public abstract class SocketMessage
{
	/*
	 * 消息接口名
	 */
	protected String interfaceName;
	
	/*
	 * 消息操作名
	 */
	protected String operationName;
	
	/*
	 * 消息数据内容
	 */
	protected ByteBuffer byteBuffer;
	
	public String getInterfaceName() 
	{
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) 
	{
		this.interfaceName = interfaceName;
	}

	public String getOperationName() 
	{
		return operationName;
	}

	public void setOperationName(String operationName) 
	{
		this.operationName = operationName;
	}

	public ByteBuffer getByteBuffer() 
	{
		return byteBuffer;
	}

	public void setByteBuffer(ByteBuffer byteBuffer) 
	{
		this.byteBuffer = byteBuffer;
	}

	/*
	 * 读取剩余数据内容
	 */
	public ByteBuffer readRemainBuffer() 
	{
		int size = byteBuffer.remaining();
		ByteBuffer buffer = ByteBuffer.allocate(size);
		//将未读完字节流按顺序写入剩余字节流
		for (int i = 0; i < size; i++) 
		{
			buffer.put(byteBuffer.get());
		}
		return buffer;
	}

	/*
	 * 写入数据内容中的剩余空间
	 */
	public boolean writeRemainBuffer(ByteBuffer byteBuffer) 
	{
		int size = byteBuffer.remaining();
		if (size > this.byteBuffer.remaining()) 
		{
			return false;
		}
		else 
		{
			for (int i = 0; i < size; i++) 
			{
				this.byteBuffer.put(byteBuffer.get());			
			}
			return true;
		}
	}

	/*
	 * 读字节
	 */
	public byte readByte() 
	{
		return byteBuffer.get();
	}

	/*
	 * 写字节
	 */
	public boolean writeByte(byte b) 
	{
		if (byteBuffer.hasRemaining()) 
		{
			byteBuffer.put(b);
			return true;			
		}
		else
		{
			return false;
		}
	}

	/*
	 * 读字节数组
	 */
	public boolean readByte(byte[] byteArray) 
	{
		int size = byteBuffer.remaining();
		int arraySize = byteArray.length;
		if (arraySize <= size) 
		{
			for (int i = 0; i < arraySize; i++) 
			{
				byteArray[i] = byteBuffer.get();
			}
			return true;
		}
		return false;
	}

	/*
	 * 写入字节数组
	 */
	public boolean writeByte(byte[] byteArray) 
	{
		int size = byteBuffer.remaining();
		int arraySize = byteArray.length;
		if (size >= arraySize) 
		{
			for (int i = 0; i < arraySize; i++) 
			{
				byteBuffer.put(byteArray[i]);
			}
			return true;
		}
		return false;
	}
	
	/*
	 * 读短整型
	 */
	public short readShort() 
	{
		return byteBuffer.getShort();
	}

	/*
	 * 写短整型
	 */
	public boolean writeShort(short data) 
	{
		//缓冲中剩余字节数大于等于2个时，才能写入短整型数值
		if (byteBuffer.remaining() >= 2) 
		{
			byteBuffer.putShort(data);
			return true;	
		}
		else 
		{
			return false;
		}
	}
	
	/*
	 * 读整型
	 */
	public int readInt() 
	{
		return byteBuffer.getInt();
	}

	/*
	 * 写整型
	 */
	public boolean writeInt(int data) 
	{
		//缓冲中剩余字节数大于等于4个时，才能写入整型数值
		if (byteBuffer.remaining() >= 4) 
		{
			byteBuffer.putInt(data);
			return true;			
		}
		else
		{
			return false;
		}
	}

	/*
	 * 读长整型
	 */
	public long readLong() 
	{
		return byteBuffer.getLong();
	}

	/*
	 * 写长整型
	 */
	public boolean writeLong(long data) 
	{
		//缓冲中剩余字节数大于等于8个时，才能写入长整型数值
		if (byteBuffer.remaining() >= 8) 
		{
			byteBuffer.putLong(data);
			return true;			
		}
		else
		{
			return false;
		}
	}
	
	/*
	 * 读字符串
	 */
	public boolean readString(StringBuffer data) 
	{
		int position = byteBuffer.position();
		int size = byteBuffer.remaining();
		List<Byte> byteList = new ArrayList<Byte>();
		for (int i = 0; i < size; i++) {
			byte b = byteBuffer.get();
			//遇到空字节，则说明一个字符串读取结束
			if (b == '\0') 
			{
				byte[] byteArray = new byte[byteList.size()];
				for (int j = 0; j < byteList.size(); j++) 
				{
					byteArray[j] = byteList.get(j);
				}
				String buffer = new String(byteArray);
				data.append(buffer);
				return true;
			}
			//将获取的字节放入临时list
			byteList.add(b);
		}
		byteBuffer.position(position);
		return false;
	}

	/*
	 * 写字符串
	 */
	public boolean writeString(StringBuffer data) 
	{
		byte[] byteArray = data.toString().getBytes();
		if (byteBuffer.remaining() >= (byteArray.length + 1)) 
		{
			for (int i = 0; i < byteArray.length; i++) 
			{
				byteBuffer.put(byteArray[i]);
			}
			byte end = '\0';
			byteBuffer.put(end);
			return true;
		}
		else
		{
			return false;
		}
	}
	
    /*
     * 校验消息合法性(解包操作)，由子类实现
     */
	public boolean validateMessage(byte[] byteArray)
	{
		return true;
	}
	
	/*
	 * 进行消息编码(打包操作)，由子类实现
	 */
	public boolean generateMessage(ByteBuffer byteBuffer)
	{
		return true;
	}
	
}
